package com.book.activeMQ.consumers;

import com.book.common.RedisUserCacheEnum;
import com.book.service.UserService;
import com.book.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import java.util.List;

/**
 * @version 1.0
 * @Author Jincheng.Li
 * @Date 2021/5/20 8:08 下午
 * @Project_Name graduation-project
 */
@Slf4j
@Component
public class RedisUserCacheConsumers implements MessageListener {
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private UserService userService;
    @Override
    public void onMessage(Message message) {
        TextMessage textMessage = (TextMessage) message;
        try {
            String text = textMessage.getText();
            if (StringUtils.isNotEmpty(text)){
                int code = Integer.parseInt(text);
                String key = RedisUserCacheEnum.get(code);
                List<String> keyList = userService.getUserListString(code);
                for (String s:keyList
                     ) {
                    redisUtil.sAdd(key,s);
                }
            }
        }catch (Exception e){
            log.error("RedisUserCacheConsumer#异常",e);
        }
    }
}
